#include "sysparamdao.h"
#include "dbutil.h"
#ifdef LINUX_DB
#include <stdio.h>
#else
#include <QSqlQuery>
#include <QVariant>
#include <QSqlError>
#include <QSqlDatabase>
#endif
SysParamDao::SysParamDao()
{
}
bool SysParamDao::updateSysParam(SysParam param)
{

    QString str("update JYDW_SYS_PARAMS set DISCOVER_GAP=%1");
    str.append(",SAMPLE_GAP=%2");
    str.append(",SNMP_TIMEOUT=%3");
    str.append(",PING_TIMEOUT=%4");
    str.append(",UPDATE_TIME=to_date('%5','yyyy-mm-dd hh24:mi:ss')");
    str.append(",TRAP_PORT=%6");
    str.append(",ALERT_SAMPLE_GAP=%7");
    str = str.arg(DbUtils::getStringFormInt(param.getFindInterval()),
                  DbUtils::getStringFormInt(param.getIngestIntervale()),
                  DbUtils::getStringFormInt(param.getSnmpTimeout()),
                  DbUtils::getStringFormInt(param.getPingTimeout()),
                  param.getUpdateTime().toString("yyyy-MM-dd hh:mm:ss"),
                  DbUtils::getStringFormInt(param.getPort()),
                  DbUtils::getStringFormInt(param.getAlartTime()));
    str.append(",use_ping_probe=%1");
    str.append(",to_probe_snmp=%2");
    str.append(",device_clean_gap=%3");
    str.append(",term_telnet_port=%4");
    str.append(",telnet_timeout=%5");
    str.append(",to_telnet_term=%6");
    str.append(",sample_data_clean_gap=%7");
    str.append(",show_style=%8");
    str.append("  where ID=%9");
    str = str.arg(DbUtils::getStringFormInt(param.getUserPingProbe()),
                  DbUtils::getStringFormInt(param.getToProbeSnmp()),
                  DbUtils::getStringFormInt(param.getOfflineTime()),
                  DbUtils::getStringFormInt(param.get_term_telnet_port()),
                  DbUtils::getStringFormInt(param.get_telnet_timeout()),
                  DbUtils::getStringFormInt(param.get_to_telnet_term()),
                  DbUtils::getStringFormInt(param.get_sample_data_clean_gap()),
                  DbUtils::getStringFormInt(param.getShowStyle()),
                  DbUtils::getStringFormInt(param.getId()));
#ifdef LINUX_DB
    char sql[500];
    DbUtils dbUtils;

    sprintf(sql, str.toLocal8Bit().data());
    int result = dbUtils.exec(sql);
    dbUtils.freeRes();
    if (result == 0)
    {
        return true;
    }
    else
    {
        DbUtils::ErrorLog("SysParamDao::updateSysParam", "SQL:" + str);
        return false;
    }
#else
    QSqlQuery query;
    query.prepare(str);
    bool result =  query.exec();
    if (!result)
    {
        DbUtils::ErrorLog("SysParamDao::updateSysParam", "SQL:" + str);
    }
    return result;
#endif
}
bool SysParamDao::insertSysParam(SysParam param)
{
    QString str("insert into JYDW_SYS_PARAMS(id,trap_port,term_telnet_port,discover_gap,sample_gap\
                ,snmp_timeout,ping_timeout,telnet_timeout,alert_sample_gap,use_ping_probe,to_probe_snmp,\
                to_telnet_term,device_clean_gap,sample_data_clean_gap\
                ,update_time,create_time,show_style) values(%1,%2,%3,%4,%5,%6,%7");
    char sql[500];
    str = str.arg(DbUtils::getStringFormInt(param.getId()),
                  DbUtils::getStringFormInt(param.getPort()),
                  DbUtils::getStringFormInt(param.get_term_telnet_port()),
                  DbUtils::getStringFormInt(param.getFindInterval()),
                  DbUtils::getStringFormInt(param.getIngestIntervale()),
                  DbUtils::getStringFormInt(param.getSnmpTimeout()),
                  DbUtils::getStringFormInt(param.getPingTimeout()));
    str.append(",%1,%2,%3,%4,%5,%6,%7");
    str = str.arg(DbUtils::getStringFormInt(param.get_telnet_timeout()),
                  DbUtils::getStringFormInt(param.getAlartTime()),
                  DbUtils::getStringFormInt(param.getUserPingProbe()),
                  DbUtils::getStringFormInt(param.getToProbeSnmp()),
                  DbUtils::getStringFormInt(param.get_to_telnet_term()),
                  DbUtils::getStringFormInt(param.getOfflineTime()),
                  DbUtils::getStringFormInt(param.get_sample_data_clean_gap()));
    str.append(",to_date('%1','yyyy-mm-dd hh24:mi:ss'),to_date('%2','yyyy-mm-dd hh24:mi:ss'),%3)");
    str = str.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
                  , QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
                  , DbUtils::getStringFormInt(param.getShowStyle()));
#ifdef LINUX_DB
    DbUtils dbUtils;
    sprintf(sql, str.toLocal8Bit().data());
    int result = dbUtils.exec(sql);
    dbUtils.freeRes();
    if (result == 0)
    {
        return true;
    }
    else
    {
        DbUtils::ErrorLog("SysParamDao::insertSysParam", "SQL:" + str);
        return false;
    }
#else
    QSqlQuery query;
    query.prepare(str);
    bool result = query.exec();
    if (!result)
    {
        DbUtils::ErrorLog("SysParamDao::insertSysParam", "SQL:" + str);
    }
    return result;
#endif
}
SysParam *SysParamDao::getParam(int id)
{
    SysParam *param = NULL;
    QString str("select id,trap_port,term_telnet_port,discover_gap,sample_gap\
                ,snmp_timeout,ping_timeout,telnet_timeout,alert_sample_gap,use_ping_probe,to_probe_snmp,\
                to_telnet_term,device_clean_gap,sample_data_clean_gap from  JYDW_SYS_PARAMS where  ID=%1");
    str = str.arg(DbUtils::getStringFormInt(id));
#ifdef LINUX_DB

    char sql[500];
    DbUtils dbUtils;
    str = str.arg(DbUtils::getStringFormInt(id));
    sprintf(sql, str.toLocal8Bit().data());
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct Param_result), "SysParamDao::getParam");
    while (result != NULL)
    {
        param = new SysParam();
        Param_result *paramResult = (Param_result *)result;
        param->setId(paramResult->id);
        param->setPort(paramResult->port);
        param->setFindInterval(paramResult->findInterval);
        param->setIngestIntervale(paramResult->ingestInterval);
        param->setSnmpTimeout(paramResult->snmpTimeout);
        param->setPingTimeout(paramResult->pingTimeout);
        param->setAlartTime(paramResult->alarTtime);
        param->setUserPingProbe(paramResult->pingTimeout);
        param->setToProbeSnmp(paramResult->to_probe_snmp);
        param->setOfflineTime(paramResult->offlineTime);
        param->set_term_telnet_port(paramResult->term_telnet_port);
        param->set_telnet_timeout(paramResult->telnet_timeout);
        param->set_to_telnet_term(paramResult->to_telnet_term);
        param->set_sample_data_clean_gap(paramResult->sample_data_clean_gap);
        break;
    }
    dbUtils.freeRes();
#else
    QSqlQuery query;
    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("SysParamDao::getParam", "SQL:" + str);
    }
    while (query.next())
    {
        param = new SysParam();
        param->setId(query.value(0).toInt());
        param->setPort(query.value(1).toInt());
        param->set_term_telnet_port(query.value(2).toInt());
        param->setFindInterval(query.value(3).toInt());
        param->setIngestIntervale(query.value(4).toInt());;
        param->setSnmpTimeout(query.value(5).toInt());
        param->setPingTimeout(query.value(6).toInt());
        param->set_telnet_timeout(query.value(7).toInt());
        param->setAlartTime(query.value(8).toInt());
        param->setUserPingProbe(query.value(9).toInt());
        param->setToProbeSnmp(query.value(10).toInt());
        param->set_to_telnet_term(query.value(11).toInt());
        param->setOfflineTime(query.value(12).toInt());
        param->set_sample_data_clean_gap(query.value(13).toInt());
        break;
    }
#endif
    return param;
}

int SysParamDao::getShowStyle()
{
    int showStyle;
    struct show_Style_struct
    {
        char showStyle;
    };
    QString str("select show_style from JYDW_SYS_PARAMS");
#ifdef LINUX_DB
    char sql[500];
    sprintf(sql, str.toLocal8Bit().data());
    DbUtils dbUtils;
    MYSQL_ROW result = dbUtils.startQuery(sql, sizeof(struct show_Style_struct), "SysParamDao::getShowStyle");
    if (result != NULL)
    {
        show_Style_struct *query_result_ptr = (show_Style_struct *)result;
        showStyle = (int)query_result_ptr->showStyle;
    }
    dbUtils.freeRes();
#else
    QSqlQuery query;

    query.prepare(str);
    if (!query.exec())
    {
        DbUtils::ErrorLog("SysParamDao::getShowStyle", "sql:" + str);
    }
    if (query.next())
    {
        showStyle = query.value(0).toInt();
    }
#endif
    return showStyle;

}
